package com.hmall.order.web;

import com.hmall.order.dto.RabbitemplateOrder;
import com.hmall.order.pojo.DownOrder;
import com.hmall.order.service.IOrderService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @PostMapping
    public Long DownOrder(@RequestBody DownOrder order) {
        long orderId = orderService.order(order);

        RabbitemplateOrder rabbitemplateOrder = new RabbitemplateOrder(orderId, order.getNum(),order.getItemId());

        rabbitTemplate.convertAndSend("order.exchange","order.key",rabbitemplateOrder);

        return orderId;
    }
}
